package com.lanhao.aot.itextpdf;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

//需导入的核心jar包
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class ITextPdfUtil {

	/**
	 * 预览二维码信息，并可批量打印
	 * 
	 * @return
	 * @throws DocumentException
	 * @throws IOException
	 */
	public void printQr() throws DocumentException, IOException {
		String path = "D:/";
		float[] rectSize = { 1, 1, 2, 2 };
		Rectangle rect = new Rectangle(114F, 85F);// A4纸张//PageSize.A4_LANDSCAPE 170F, 85.0F // 200,120
		rect.setBackgroundColor(BaseColor.WHITE);
		BaseFont bfChinese2 = BaseFont.createFont(path + "/fonts/SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
		Font fang16 = new Font(bfChinese2, 8, Font.NORMAL);
		// 左侧固定信息 - 如 资产名称、资产编号、购置日期、使用人 等
		BaseFont bfChinese3 = BaseFont.createFont(path + "/fonts/SIMKAI.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
		Font kai16 = new Font(bfChinese3, 8, Font.NORMAL);

		Document doc = new Document(rect, rectSize[0], rectSize[1], rectSize[2], rectSize[3]);

		PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream("D:/fonts/htmltest.pdf"));

		writer.setPdfVersion(PdfWriter.PDF_VERSION_1_2);
		// 文档属性
		doc.addTitle("资产信息");
		doc.open();

		// 加入内容
		Map<String, String> paramMap = new TreeMap<String, String>();
		paramMap.put("资产编号", "TY2015000029");
		paramMap.put("资产名称", "海信电视");
		paramMap.put("获取日期", "2020-06-08");
		paramMap.put("使用部门", "售后1部");
		paramMap.put("使用人", "曹志");
		createPrintQrInfo(doc, paramMap, fang16, kai16);
		doc.newPage();
		doc.close();
	}

	private static void createPrintQrInfo(Document doc, Map<String, String> paramMap,
			Font valueFont, Font kai16)
			throws DocumentException {
		// 这里打印的是 40mm * 30mm 大小的标签，故这里参数很重要
		int[] cellWidth = { 40, 74 };
		PdfPTable tab = new PdfPTable(2);
		tab.setLockedWidth(true);
		tab.setTotalWidth(110);
		tab.getDefaultCell().setMinimumHeight(17f);
		tab.setHorizontalAlignment(Element.ALIGN_CENTER);
		tab.setWidths(cellWidth);
		PdfPCell cell = null ; 
		Iterator<Map.Entry<String ,String>> it = paramMap.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry<String, String> entry = it.next();
			cell = PdfCellUtill.createCell(entry.getKey(), kai16, PdfPCell.ALIGN_LEFT, PdfPCell.ALIGN_MIDDLE, 1, 0);
			cell.setFixedHeight(16f);
			tab.addCell(cell);
			cell = PdfCellUtill.createCell(entry.getValue(), valueFont, PdfPCell.ALIGN_LEFT, PdfPCell.ALIGN_MIDDLE, 1, 0);
			cell.setFixedHeight(16f);
			tab.addCell(cell);
		}

		doc.add(tab);
	}

	public static void main(String[] args) {
		ITextPdfUtil iTextPdfUtil = new ITextPdfUtil();
		try {
			iTextPdfUtil.printQr();
		} catch (DocumentException | IOException e) {
			e.printStackTrace();
		}
	}

}
